#pragma once
#include "GameComponent.h"
#include "Messenger.h"
#include "Event_Control_Changed.h"
#include "Timer.h"
#include "PlayerController.h"




class CompCommandListener : public Boon::GameComponent
{
	GAMECOMPONENT( CompCommandListener, GameComponent )

public:
	virtual void OnCreate() 
	{

	}

	virtual void OnStart() 
	{
		m_Transform = &GetGameObject().GetTransform();
		if( !GetGameObject().GetComponent<PlayerController>().Empty())
			m_PlayerController = GetGameObject().GetComponent<PlayerController>().Lock().Get();
		BOON_ASSERT( m_PlayerController != nullptr )
	}

	virtual void OnEnable() 
	{
		Boon::EventListener listener( this, &CompCommandListener::OnReceiveCommandEvent );
		Boon::Messenger::GetInstance().Register( Event_Control_Changed::ID, listener );
	}

	virtual void OnDisable() 
	{
		Boon::EventListener listener( this, &CompCommandListener::OnReceiveCommandEvent );
		Boon::Messenger::GetInstance().Remove( Event_Control_Changed::ID, listener );
	}
	
	virtual void Update() 
	{
	}

	virtual void LateUpdate() 
	{
		
	}

	virtual void OnDestroy() 
	{
		
	}

	void OnReceiveCommandEvent( const Boon::IMessengerEventStrong& eventData )
	{
		Event_Control_Changed* event = static_cast<Event_Control_Changed*>( eventData.Get() );

		if( event->m_Jump->IsRaised() )
			m_PlayerController->Jump();
		if( event->m_Attack->IsRaised() )
			m_PlayerController->Attack();
		if( event->m_Left->IsRaised() || event->m_Left->IsHolding() )
			m_PlayerController->WalkLeft();
		else if( event->m_Right->IsRaised() || event->m_Right->IsHolding() )
			m_PlayerController->WalkRight();
	}

private:
	Boon::Transform*					m_Transform;
	PlayerController*					m_PlayerController;
};

